import { translate, Language } from './i18n';

// 创建一个函数用于生成全局可见的中英文混合文本
export function globalText(key: string): string {
    return `${translate(key, 'zh')} ${translate(key, 'en')}`;
}

// world.querySelectorAll('player').forEach((e) => {if(e.player.name !=='坦率的血翼蝠5801')return;e.addTag('地铁')})
//  world.querySelectorAll('player').forEach((e) => {e.player.link('https://dao3.fun/edit/8a2109ec58264c4f2805')})
const ks = world.querySelector('#开锁器')
if (ks) {
    ks.enableInteract = true
    ks.interactRadius = 3
    ks.interactHint = globalText('开锁器')
    ks.onInteract(async ({ entity }) => {
        if (entity.bq.includes('开锁能力')) return
        entity.player.directMessage(translate('你获得了开锁器，现在开始，大部分锁的门都可以直接进入了！', entity?.language || 'zh'))
        entity.addTag('ks')
        entity.bq.push('开锁能力')
    })
}


const lz = world.querySelector('#方向盘')
if (lz) {
    lz.enableInteract = true
    lz.interactRadius = 5
    lz.interactHint = globalText('方向盘')

    lz.onInteract(async ({ entity }) => {
        if (entity.hasTag('火')) {
            entity.player.directMessage(translate('已获得方向盘×1，{onHand}放回原位', entity?.language || 'zh', { onHand: entity.onHand }))
            entity.onHand = translate('方向盘', entity?.language || 'zh')
        } else {
            entity.player.directMessage(translate('OUCH！', entity?.language || 'zh'))
        }
    })
}
const tree = world.querySelector('#木板-1')
if (tree) {
    tree.enableInteract = true
    tree.interactRadius = 5
    tree.interactHint = globalText('木板')

    tree.onInteract(async ({ entity }) => {
        if (entity.onHand == '玻璃保险丝') {
            entity.removeTag('noTree')
            entity.bq.push('走三楼木板')
            entity.player.directMessage(translate('放置成功！', entity?.language || 'zh'))
        } else {
            entity.player.directMessage(translate('我还需要一根玻璃保险丝', entity?.language || 'zh'))
        }
    })
}
const button = world.querySelector('#按钮')
if (button) {
    button.enableInteract = true
    button.interactRadius = 5
    button.interactHint = globalText('按钮')

    button.onInteract(async ({ entity }) => {
        if (entity.key == 3) {
            entity.removeTag('noBridge')
            entity.bq.push('走桥')
            entity.player.directMessage(translate('一个巨大的声音从远处传来……', entity?.language || 'zh'))
        } else {
            entity.player.directMessage(translate('钥匙不足，无法奏效。当前钥匙{key}/3个', entity?.language || 'zh', { key: entity.key }))
        }
    })
}
const fire = world.querySelector('#火')
if (fire) {
    fire.enableInteract = true
    fire.interactRadius = 5
    fire.interactHint = globalText('壁炉')

    fire.onInteract(async ({ entity }) => {
        entity.addTag('火')
        entity.bq.push('生火成功！')
        entity.player.directMessage(translate('呼噜！你试着生火，成功了！', entity?.language || 'zh'))
    })
}
const dead = world.querySelector('#尸体')
if (dead) {
    dead.enableInteract = true
    dead.interactRadius = 4
    dead.interactHint = globalText('检查')
    dead.onInteract(async ({ entity }) => {
        if (entity.wq > 0) {
            entity.player.directMessage(translate('他身上什么也没有了，你已经夺去了他的唯一（霰弹枪×1）', entity?.language || 'zh'))
        }
        entity.player.directMessage(translate('你在他身上找到了一把霰弹枪！获得霰弹枪×1', entity?.language || 'zh'))
        // entity.onHand = '霰弹枪'
        entity.wq = 5
        if (entity.bq.includes('攻击性')) {
            return
        } else {
            entity.bq.push('攻击性')
        }
    })
}
const fx = world.querySelector('#方向盘')
if (fx) {
    fx.enableInteract = true
    fx.interactRadius = 8
    fx.interactHint = globalText('方向盘')

    fx.onInteract(async ({ entity }) => {
        if (entity.onHand == `方向盘`) {
            entity.addTag('go')
            entity.bq.push('逃离能力')
            entity.player.directMessage(translate('你用手上的方向盘吸附在了上面，旋转了！', entity?.language || 'zh'))
            entity.onHand = '空气'
        } else {
            entity.player.directMessage(translate('我还需要 方向盘×1', entity?.language || 'zh'))
        }
    })
}

const gs = world.querySelector('#钢丝')
if (gs) {
    gs.enableInteract = true
    gs.interactRadius = 5
    gs.interactHint = globalText('玻璃保险丝')
    gs.onInteract(async ({ entity }) => {
        entity.player.directMessage(translate('已获得玻璃保险丝×1，{onHand}放回原位', entity?.language || 'zh', { onHand: entity.onHand }))
        entity.onHand = '玻璃保险丝'
    })
}
const electric = world.querySelector('#电梯')

world.querySelector('.door')?.onEntityContact(async ({ other, entity }) => {
    if (!other || other === null) return;
    if (!(other.player)) return;
    if (other.hasTag('go')) {
        world.say(`${other.player.name} 逃离了这里！获得发光、飞行能力！ {player} escaped from here! Gained light emission and flight abilities!`.replace('{player}', other.player.name))
        other.hp = other.maxHp
        other.player.emissive = 1
        other.player.canFly = true
        other.player.spectator = true
        other.removeTag('吸引')
        other.bq.push('不被敌人看见')
        other.position.copy(other.player.spawnPoint)
        other.wq = 0
    } else {
        other.player.directMessage(translate('门上锁着……', other?.language || 'zh'))
    }
})

world.querySelector('#木屋门')?.onEntityContact(async ({ other, entity }) => {
    if (!(other.player)) return;
    other.player.directMessage(translate('你还需要 木屋钥匙', other?.language || 'zh'))
})

const yz = world.querySelector('#椰子')
if (yz) {
    yz.enableInteract = true
    yz.interactRadius = 5
    yz.interactHint = globalText('椰子')
    yz.onInteract(async ({ entity }) => {
        entity.player.directMessage(translate('已获得椰子×1，{onHand}放回原位', entity?.language || 'zh', { onHand: entity.onHand }))
        entity.onHand = '椰子'
    })
}


const dt = world.querySelector('#断头台')
if (dt) {
    dt.enableInteract = true
    dt.interactRadius = 5
    dt.interactHint = globalText('断头台')
    dt.onInteract(async ({ entity }) => {
        if (entity.onHand == '椰子') {
            entity.player.directMessage(translate('从中跌落泰迪熊×1，已拾取，{onHand}放回原位', entity?.language || 'zh', { onHand: entity.onHand }))
            entity.onHand = '泰迪熊'
        } else {
            entity.player.directMessage(translate('这里还需要一个球形的东西', entity?.language || 'zh'))
        }
    })
}

const yl = world.querySelector('#摇篮')
if (yl) {
    yl.enableInteract = true
    yl.interactRadius = 5
    yl.interactHint = globalText('兰德里纳河的摇篮')
    yl.onInteract(async ({ entity }) => {
        if (entity.onHand == '泰迪熊') {
            entity.player.directMessage(translate('你把泰迪熊放入了摇篮，兰德里纳河似乎出现了……她不会再伤害你了……', entity?.language || 'zh'))
            entity.onHand = '空气'
            entity.addTag('摇篮')
            entity.bq.push('兰德里纳河不再打')
        } else {
            entity.player.directMessage(translate('这个摇篮温馨而恐怖……需要什么东西放在其中……', entity?.language || 'zh'))
        }
    })
}
const coin = world.querySelector('#小硬币')
if (coin) {
    coin.enableInteract = true
    coin.interactRadius = 2
    coin.interactHint = globalText('保险箱')
    coin.onInteract(async ({ entity }) => {
        if (entity.hasTag('安全钥匙')) {
            entity.player.directMessage(translate('已获得小硬币×1，{onHand}放回原位', entity?.language || 'zh', { onHand: entity.onHand }))
            entity.onHand = `小硬币`
        } else {
            entity.player.directMessage(translate('你还需要安全钥匙', entity?.language || 'zh'))
        }
    })
}
const computer = world.querySelector('#电脑')
if (computer) {
    computer.enableInteract = true
    computer.interactRadius = 2
    computer.interactHint = globalText('生锈的检票机器')
    computer.onInteract(async ({ entity }) => {
        if (entity.onHand == `小硬币`) {
            entity.addTag('地铁')
            entity.bq.push('投币成功')
            entity.onHand = `空气`
            entity.player.directMessage(translate('你把硬币塞入，现在你可以乘坐地铁了！', entity?.language || 'zh'))
        } else {
            entity.player.directMessage(translate('这里需要塞个什么东西进去（投币口）', entity?.language || 'zh'))
        }
    })
}
const you = world.querySelector('#油箱')
if (you) {
    you.enableInteract = true
    you.interactRadius = 2
    you.interactHint = globalText('废弃许久的油箱')
    you.onInteract(async ({ entity }) => {
        if (entity.onHand == `汽油`) {
            if (entity.hasTag('地铁')) {
                entity.onHand = `空气`
                entity.player.directMessage(translate('地铁可以发动了，你已经充好油了！', entity?.language || 'zh'))
                await sleep(1000)
                entity.player.directMessage(translate('即将发动！5……', entity?.language || 'zh'))
                await sleep(1000)
                entity.player.directMessage(translate('即将发动！4……', entity?.language || 'zh'))
                await sleep(1000)
                entity.player.directMessage(translate('即将发动！3……', entity?.language || 'zh'))
                await sleep(1000)
                entity.player.directMessage(translate('即将发动！2……', entity?.language || 'zh'))
                await sleep(1000)
                entity.player.directMessage(translate('即将发动！1……', entity?.language || 'zh'))
                await sleep(1000)
                entity.player.directMessage(translate('发动！', entity?.language || 'zh'))
                await sleep(640)
                entity.velocity.z -= 25
                await sleep(800)
                entity.position.z -= 6
            }
        } else {
            entity.player.directMessage(translate('你还需要汽油', entity?.language || 'zh'))
        }
    })
}

const you1 = world.querySelector('#加油')
if (you1) {
    you1.enableInteract = true
    you1.interactRadius = 2
    you1.interactHint = globalText('发霉的汽油')
    you1.onInteract(async ({ entity }) => {
        if (entity.onHand == `汽油`) return;
        entity.player.directMessage(translate('已获得汽油×1，{onHand}放回原位', entity?.language || 'zh', { onHand: entity.onHand }))
        entity.onHand = `汽油`

    })
}
world.querySelector('.subway')?.onEntityContact(async ({ other, entity }) => {
    if (!other || other === null) return;
    if (!(other.player)) return;
    world.say(`${other.player.name} 通过地铁逃离了这里！获得发光、飞行能力！ {player} escaped via subway! Gained light emission and flight abilities!`.replace('{player}', other.player.name))
    other.player.emissive = 1
    other.player.canFly = true
    other.player.spectator = true
    other.hp = other.maxHp
    other.removeTag('吸引')
    entity.bq.push('不被敌人看见')
    other.wq = 0
})

world.querySelector('.subway1')?.onEntityContact(async ({ other, entity }) => {
    if (!other || other === null) return;
    if (!(other.player)) return;
    world.say(`${other.player.name} 通过地铁逃离了这里！获得发光、飞行能力！ {player} escaped via subway! Gained light emission and flight abilities!`.replace('{player}', other.player.name))
    other.player.emissive = 1
    other.player.canFly = true
    other.player.spectator = true
    other.hp = other.maxHp
    other.removeTag('吸引')
    entity.bq.push('不被敌人看见')
    other.wq = 0
})

world.querySelector('.subway2')?.onEntityContact(async ({ other, entity }) => {
    if (!other || other === null) return;
    if (!(other.player)) return;
    world.say(`${other.player.name} 通过地铁逃离了这里！获得发光、飞行能力！ {player} escaped via subway! Gained light emission and flight abilities!`.replace('{player}', other.player.name))
    other.player.emissive = 1
    other.hp = other.maxHp
    other.player.canFly = true
    other.player.spectator = true
    other.removeTag('吸引')
    entity.bq.push('不被敌人看见')
    other.wq = 0
})

const car = world.querySelector('#汽车')
if (car) {
    car.enableInteract = true
    car.interactRadius = 5
    car.interactHint = globalText('汽车')
    car.onInteract(async ({ entity }) => {
        if (entity.onHand == `电缆`) return
        if (entity.hasTag('汽车')) {
            entity.player.directMessage(translate('已获得电缆×1，{onHand}放回原位', entity?.language || 'zh', { onHand: entity.onHand }))
            entity.onHand = '电缆'
        }
    })
}
const cl = world.querySelector('#齿轮挂件-1')
if (cl) {
    cl.enableInteract = true
    cl.interactRadius = 5
    cl.interactHint = globalText('齿轮排')
    cl.onInteract(async ({ entity }) => {
        if (entity.onHand == `电缆`) {
            entity.player.directMessage(translate('你获得了麻醉针，一发入魂！', entity?.language || 'zh'))
            entity.wq = 100
        } else {
            entity.player.directMessage(translate('我还需要一根电缆', entity?.language || 'zh'))
        }
    })
}

const dg = world.querySelector('#武器盒')
if (dg) {
    dg.enableInteract = true
    dg.interactRadius = 5
    dg.interactHint = globalText('盒子')
    dg.onInteract(async ({ entity }) => {
        if (entity.onHand == `武器钥匙`) {
            entity.player.directMessage(translate('你获得了弹弓！', entity?.language || 'zh'))
            entity.wq = 20
            if (entity.bq.includes('攻击性')) {
                return
            } else {
                entity.bq.push('攻击性')
            }
        } else {
            entity.player.directMessage(translate('我还需要武器钥匙', entity?.language || 'zh'))
        }
    })
}

// 找到NPC，id名叫“普通模式”
const npc = world.querySelector('#普通模式');
if (npc) {
    // 让这个NPC开启互动功能
    npc.enableInteract = true;

    // 互动距离为3个单位
    npc.interactRadius = 3;

    // 互动文字“前往正常模式”
    npc.interactHint = globalText('前往正常模式');

    // 当互动时
    npc.onInteract(async ({ entity }) => {
        // 弹出选项对话框（SELECT），询问是否进入
        const result = await entity.player.dialog({
            type: GameDialogType.SELECT,
            title: translate('正常模式', entity?.language || 'zh'),
            lookTarget: npc,
            content: translate('{player}，你想尝试挑战这个普通模式吗？', entity?.language || 'zh', { player: entity.player.name }),
            options: [translate('是', entity?.language || 'zh'), translate('否', entity?.language || 'zh')],
        });
        if (!result || result == null) return;
        // 如果点击‘是’，玩家传送至拓展地图，地图ID：100183839
        if (result.index === 0) {
            entity.player.link('https://dao3.fun/play/813da8c2cf1572969d2a', { isConfirm: false, isNewTab: false })
        }
    });
}

const hb = world.querySelector('#海报')
if (hb) {
    hb.enableInteract = true
    hb.interactRadius = 5
    hb.interactHint = globalText('分享地图')

    hb.onInteract(async ({ entity }) => {
        entity.player.navigator.emitEvent('share', {
            title: translate('分享', entity?.language || 'zh'),
            description: translate('description', entity?.language || 'zh'),
            text: translate('让这一份惊恐分享给更多人', entity?.language || 'zh') + `\nhttps://dao3.fun/exp/experience/detail/100174245`,
        })
    })
}




